package com.live.dengdengtv.activity.login;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.live.dengdengtv.BaseActivity;
import com.live.dengdengtv.R;
import com.live.dengdengtv.bean.AppConstants;
import com.live.dengdengtv.kernel.presenters.InitBusinessHelper;
import com.live.dengdengtv.kernel.utils.SxbLogImpl;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.umeng.socialize.PlatformConfig;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import cn.jpush.android.api.JPushInterface;
import okhttp3.OkHttpClient;

/**
 * Created by wyf on 2016/8/5.
 */
public class GuideActivity extends BaseActivity{

    private ArrayList<ImageView> ivList = new ArrayList<ImageView>();;

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(isInstalled()){
            setLauncherView();
        }else{
            setFistInstallView();
        }
    }

    private void setLauncherView() {
        setContentView(R.layout.activity_launch);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(context, LoginActivity.class);
//                Intent intent = new Intent(context, LoginWayActivity.class);
                startActivity(intent);
                finish();
            }
        }, 2000);

        holdLogin();
    }

    private void setFistInstallView() {

        setContentView(R.layout.activity_guide);

        ivList = new ArrayList<ImageView>();;

        ImageView imageView = getImageView();
        imageView.setImageResource(R.mipmap.guide_1);
        ivList.add(imageView);
        imageView = getImageView();
        imageView.setImageResource(R.mipmap.guide_2);
        ivList.add(imageView);
        imageView = getImageView();
        imageView.setImageResource(R.mipmap.guide_3);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setLauncherView();
            }
        });
        ivList.add(imageView);

        ViewPager viewpager = (ViewPager) findViewById(R.id.viewpager);
        viewpager.setAdapter(new MyPagerAdapter());
    }

    private void holdLogin(){
        SharedPreferences sp = context.getSharedPreferences(AppConstants.SP_LOGIN, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("InstallFist",true);
        editor.commit();
    }

    private boolean isInstalled(){
        SharedPreferences sp = context.getSharedPreferences(AppConstants.SP_LOGIN, Context.MODE_PRIVATE);
        return sp.getBoolean("InstallFist",false);
    }

    private ImageView getImageView(){
        ImageView imageView = new ImageView(context);
        imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        return  imageView;
    }


    private class MyPagerAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return ivList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            container.addView(ivList.get(position));
            return ivList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(ivList.get(position));
        }
    }
}
